/**
 * gislin 2017��7��13��
 */
package com.btcode.orm.model;

import com.btcode.db.fieldtype.FieldType;
import java.io.Serializable;

/**
 * @author gislin 2017��7��13��
 */
public class Field implements IField, Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    String columnName;

    FieldType fileType;

    boolean isForUpdate = true;

    boolean isKey = false;

    boolean isUnique = false;

    Class<IModel> modelClass;

    public <T extends IModel> Field(Class<T> model, String columnName, FieldType fieldType) {
        init(model, columnName, fieldType, false, true, false);
    }

    public <T extends IModel> Field(Class<T> model, String columnName, FieldType fieldType,
        boolean isKey) {
        init(model, columnName, fieldType, isKey, true, false);
    }

    public <T extends IModel> Field(Class<T> model, String columnName, FieldType fieldType,
        boolean isKey, boolean isForUpdate) {
        init(model, columnName, fieldType, isKey, isForUpdate, false);
    }

    public <T extends IModel> Field(Class<T> model, String columnName, FieldType fieldType,
        boolean isKey, boolean isForUpdate, boolean isUnique) {
        init(model, columnName, fieldType, isKey, isForUpdate, isUnique);
    }

    @SuppressWarnings("unchecked")
    public <T extends IModel> void init(Class<T> model, String columnName, FieldType fieldType,
        boolean isKey, boolean isForUpdate, boolean isUnique) {
        this.modelClass = (Class<IModel>) model;
        this.columnName = columnName.toUpperCase();
        this.fileType = fieldType;
        this.isKey = isKey;
        this.isForUpdate = isForUpdate;
        this.isUnique = isUnique;
    }

    @Override
    public boolean isKey() {
        return isKey;
    }

    @Override
    public String getTableName() {
        IModel model;
        try {
            model = modelClass.newInstance();
            return model.getTableName();
        }
        catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getFieldName() {
        return columnName;
    }

    @Override
    public FieldType getFieldType() {
        return fileType;
    }

    @Override
    public Class<IModel> getModelClass() {
        return modelClass;
    }

    @Override
    public String toString() {
        String str =
            "fileType:" + fileType + ",class:" + modelClass.getName() + ",name:" + columnName
                + "\r\n";
        return str;
    }

    @Override
    public boolean isForUpdate() {
        return isForUpdate;
    }

    @Override
    public boolean isUnique() {
        return isUnique;
    }

}